package com.thuanpq.database.adapter;

import com.thuanpq.entity.IncomeEntity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class BlackIncomeAdapter {
	private DatabaseAdapter mDbAdapter;

	public BlackIncomeAdapter(Context context) {
		mDbAdapter = new DatabaseAdapter(context);
	}

	public boolean insertBlackIncome(IncomeEntity entity) {
		if (entity != null) {
			ContentValues inititalValues = new ContentValues();
			inititalValues.put(IncomeEntity.PHONE_NUMBER,
					entity.getPhoneNumber());
			inititalValues.put(IncomeEntity.CALL_DATE, entity.getCalledDate());

			boolean result = mDbAdapter.insert(IncomeEntity.TABLE_NAME, null,
					inititalValues) > 0;
			return result;
		}
		return false;
	}

	public boolean deleteBlackIncomeByNumber(String number) {
		return mDbAdapter.delete(IncomeEntity.TABLE_NAME,
				IncomeEntity.PHONE_NUMBER + "=?", new String[] { number }) > 0;
	}

	public boolean deleteAllBlackIncome() {
		return mDbAdapter.delete(IncomeEntity.TABLE_NAME, null, null) > 0;
	}

	public IncomeEntity[] getAllBlackIncome() {

		IncomeEntity[] units = null;
		Cursor cur = mDbAdapter.query(IncomeEntity.TABLE_NAME, null, null,
				null, null, null, null);
		if (cur != null) {
			units = new IncomeEntity[cur.getCount()];
			int i = 0;
			while (!cur.isAfterLast()) {
				units[i++] = getIncomeEntity(cur);
				cur.moveToNext();
			}
			cur.close();
		}
		return units;
	}

	private IncomeEntity getIncomeEntity(Cursor cur) {
		if (cur != null) {
			IncomeEntity values = new IncomeEntity();

			values.setPhoneNumber(cur.getString(cur
					.getColumnIndex(IncomeEntity.PHONE_NUMBER)));

			values.setPhoneNumber(cur.getString(cur
					.getColumnIndex(IncomeEntity.CALL_DATE)));
			return values;
		}
		return null;
	}
}